package com.xiyou.luckdraw.test;

import com.xiyou.luckdraw.api.dal.entitys.AnswerBank;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ConvertTxtExcel
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-18 17:43
 */
public class ConvertTxtExcel {

    private static List<AnswerBank> list = new ArrayList<AnswerBank>();
    private final static String TXTFILENAME = "C:\\Users\\ASUS\\Desktop\\题目\\和平精英补题1017-100-2.txt";
    private final static String EXCELFILENAME = "C:\\Users\\ASUS\\Desktop\\题目\\heping.xls";

    public static void main(String[] args) {
        getTxt();
        txtToExcel();
    }

    private static void txtToExcel() {
        WritableWorkbook wwk = null;
        try {
            wwk = Workbook.createWorkbook(new File(EXCELFILENAME));
            WritableSheet ws = wwk.createSheet("Test1", 0);

            /*
             * 生成一个保存数字的单元格 必须使用Number的完整包路径，否则有语法歧义
             */
            for (int i = 0; i < list.size(); i++) {
                AnswerBank answer = list.get(i);
                Label label1 = new Label(0, (i + 1), answer.getAnswerTitle());
                Label label2 = new Label(1, (i + 1), answer.getA());
                Label label3 = new Label(2, (i + 1), answer.getB());
                Label label4 = new Label(3,  (i + 1), answer.getC());
                Label label5 = new Label(4,  (i + 1), answer.getD());
                Label label6 = new Label(5,  (i + 1), answer.getCorrectAnswer()+"");
                Label label7 = new Label(6,  (i + 1), answer.getLevel()+"");
                ws.addCell(label1);
                ws.addCell(label2);
                ws.addCell(label3);
                ws.addCell(label4);
                ws.addCell(label5);
                ws.addCell(label6);
                ws.addCell(label7);
            }
            wwk.write();
            wwk.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
        System.out.println("完成");
    }

    private static List<AnswerBank> getTxt() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(TXTFILENAME), "UTF-8"));

            String line = "";
            int i = 0;
            while ((line = br.readLine()) != null) {
                //TXT里面的数据都是用-隔开的，例如：1-haha-123123-12
                String[] arr = line.split(",");
                AnswerBank answer = new AnswerBank();
                if (null != arr) {
                    i++;
                    System.out.println("-------------"+ i+"========"+ arr[0].substring(1, arr[0].length() - 1));
                    answer.setAnswerTitle(arr[0].substring(1, arr[0].length() - 1));
                    answer.setA(arr[1].substring(1, arr[1].length() - 1));
                    answer.setB(arr[2].substring(1, arr[2].length() - 1));
                    answer.setC(arr[3].substring(1, arr[3].length() - 1));
                    answer.setD(arr[4].substring(1, arr[4].length() - 1));
                    answer.setCorrectAnswer(new Long(arr[5].substring(1, arr[5].length() - 1)));
                    answer.setLevel(new Long(arr[6].substring(1, arr[6].length() - 1)));
                }
                //    answer.setAnswerTitle();
                list.add(answer);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}
